
package com.job.client.common.utils;


import com.aliyun.tea.TeaException;
import lombok.extern.slf4j.Slf4j;

import java.util.Random;


@Slf4j
public class SendSmsToAly {



    /*
    * 阿里云发送短信业务
    * 因为使用测试用例，所以手机号固定
            DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",
                System.getenv("LTAI5tHTeBg826AMHTGGs7C6"),
                System.getenv("qaETZyptZRwiTzOWruKWVdKxa7MkvS"));
    * */
    public static String sendSmsCode(String phone) throws Exception {
        // 请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID 和 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
        // 工程代码泄露可能会导致 AccessKey 泄露，并威胁账号下所有资源的安全性。以下代码示例仅供参考，建议使用更安全的 STS 方式，更多鉴权访问方式请参见：https://help.aliyun.com/document_detail/378657.html
        com.aliyun.dysmsapi20170525.Client client =
                createClient("LTAI5tHTeBg826AMHTGGs7C6",
                        "qaETZyptZRwiTzOWruKWVdKxa7MkvS");
        com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest =
                new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
                .setSignName("阿里云短信测试")
                .setTemplateCode("SMS_154950909")
                .setPhoneNumbers(phone)
                .setTemplateParam("{\"code\":\"1234\"}");
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        try {
            com.aliyun.dysmsapi20170525.models.SendSmsResponse resp = client.sendSmsWithOptions(sendSmsRequest, runtime);
            com.aliyun.teaconsole.Client.log(com.aliyun.teautil.Common.toJSONString(resp));
        } catch (TeaException error) {
            // 错误 message
            System.out.println(error.getMessage());
            // 诊断地址
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            System.out.println("An error occurred: " + _error.getMessage());
            _error.printStackTrace(); // 打印堆栈跟踪到标准错误流
//            TeaException error = new TeaException(_error.getMessage(), _error);
//            // 错误 message
//            System.out.println(error.getMessage());
//            // 诊断地址
//            System.out.println(error.getData().get("Recommend"));
//            com.aliyun.teautil.Common.assertAsString(error.message);
        }
        return "1234";
    }

    /**
     * 使用AK&SK初始化账号Client
     * @param accessKeyId
     * @param accessKeySecret
     * @return Client
     * @throws Exception
     */
    public static com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // 必填，您的 AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 必填，您的 AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new com.aliyun.dysmsapi20170525.Client(config);
    }


    /*
    * 阿里云发送短信业务例子
    * */
    public static String sendSmsCodeExample(String phone){
        log.info(phone+":发送了短信");
        String code = getCode();
        return code;
    }
    private static String getCode(){
        Random rand = new Random();
        int num = rand.nextInt(9000) + 1000;
        return String.valueOf(num);
    }

}